package com.shaoshaohuo.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.PurchaseRecord;
import com.shaoshaohuo.app.net.HttpRequest;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.PublishManagerActivity;
import com.shaoshaohuo.app.ui.ec.BuysGoodsActivity;
import com.shaoshaohuo.app.utils.AnimationUtil;
import com.shaoshaohuo.app.utils.AreaUtil;
import com.shaoshaohuo.app.utils.StringHelper;
import com.shaoshaohuo.app.utils.ToastUtil;
import com.shaoshaohuo.app.view.MessageDialog;

import java.util.List;

public class PurchaseRecordListAdapter<T> extends MyBaseAdapter<PurchaseRecord> implements View.OnClickListener {

	private static final int TAG_UPDATE_TEXT_VIEW = 10;

	public PurchaseRecordListAdapter(Context ctx, List<PurchaseRecord> list, boolean flag) {
		super(ctx, list, flag);
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {

		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.view_purchase_record_list_item, null);
			holder = new ViewHolder();
			holder.titleText = (TextView) convertView.findViewById(R.id.textview_name);
			holder.countText = (TextView) convertView.findViewById(R.id.textview_count);
			holder.addressText = (TextView) convertView.findViewById(R.id.textview_address);
			holder.endTimeText = (TextView) convertView.findViewById(R.id.textview_end_time);
			holder.createDate = (TextView) convertView.findViewById(R.id.textview_cdate);
			holder.freshLayout = (LinearLayout) convertView.findViewById(R.id.layout_fresh);
			holder.editLayout = (LinearLayout) convertView.findViewById(R.id.layout_edit);
			holder.deleteLayout = (LinearLayout) convertView.findViewById(R.id.layout_delete);
			holder.browseCount = (TextView) convertView.findViewById(R.id.textview_browse_count);
			holder.quoteCount = (TextView) convertView.findViewById(R.id.tv_notice_count);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		PurchaseRecord item = list.get(pos);

		holder.titleText.setText(item.getCatname());
		holder.countText.setText(item.getNum()+item.getUnit());
		holder.addressText.setText(AreaUtil.getAreaFullname(item.getCitycode()) + item.getAddress());
		if(TextUtils.isEmpty(item.getEnd()) || "0".equals(item.getEnd())){
			holder.endTimeText.setText("无");
		}else{
			holder.endTimeText.setText(StringHelper.formatDateDay(item.getEnd()));
		}
		holder.createDate.setText("发布日期：" + StringHelper.formatDateMinute(item.getUdate()));
		holder.browseCount.setText("浏览量：" + item.getVnum() + "次");
		holder.quoteCount.setText(item.getQuoteCount() + "人");
		holder.freshLayout.setTag(R.id.textview_cdate, holder.createDate);
		holder.freshLayout.setTag(item.getNumber());
		holder.editLayout.setTag(item.getId());
		holder.deleteLayout.setTag(item.getNumber());
		holder.freshLayout.setOnClickListener(this);
		holder.editLayout.setOnClickListener(this);
		holder.deleteLayout.setOnClickListener(this);
//		 detailText.setonc

		return convertView;
	}

	@Override
	public void onClick(final View v) {
		int viewId = v.getId();
		switch (viewId) {
			case R.id.layout_fresh: {
				String recordId = (String) v.getTag();
				View updateTV = (View) v.getTag(R.id.textview_cdate);

				requestDataFresh(recordId, updateTV);
			}
			break;
			case R.id.layout_edit: {
				String goodsId = (String) v.getTag();

				Intent intent = new Intent(context, BuysGoodsActivity.class);
				intent.putExtra(BuysGoodsActivity.EXTRA_RECORD_ID, goodsId);
				context.startActivity(intent);
			}
			break;
			case R.id.layout_delete: {
				MessageDialog dialog = new MessageDialog(context);
				dialog.setMessage("确认要删除吗？");
				dialog.setLeftBtText("取消");
				dialog.setRightBtText("确认");
				dialog.setOnBtClickListener(new MessageDialog.OnBtClickListener() {
					@Override
					public void onLeftBtClick() {
					}

					@Override
					public void onRightBtClick() {
						String goodsId = (String) v.getTag();
						requestDataDelete(goodsId);
					}
				});
				dialog.show();
			}
			break;
			default:
				break;
		}
	}

	private void requestDataFresh(String recordId, final View updateTV) {
		RequestService.getInstance().publishedPurchaseRecordRefresh(context, recordId, BaseEntity.class, new HttpRequest.RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				if (resultData.isOk()) {
					((TextView) updateTV).setText("发布日期：" + StringHelper.formatDateMinute("" + System.currentTimeMillis()));
					AnimationUtil.emphasize(context, updateTV, 0);
					ToastUtil.toast("刷新置顶成功");
				} else {
					ToastUtil.toast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				ToastUtil.toast(msg);
			}
		});
	}



	private void requestDataDelete(String recordId) {
		RequestService.getInstance().publishedPurchaseRecordDelete(context, recordId, BaseEntity.class, new HttpRequest.RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				if (resultData.isOk()) {
					ToastUtil.toast("删除成功");
					((PublishManagerActivity) context).onRefresh();
				} else {
					ToastUtil.toast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				ToastUtil.toast(msg);
			}
		});
	}

	static class ViewHolder {
		TextView titleText;
		TextView countText;
		TextView addressText;
		TextView endTimeText;
		TextView createDate;
		TextView browseCount;
		TextView quoteCount;
		LinearLayout freshLayout;
		LinearLayout editLayout;
		LinearLayout deleteLayout;
	}

}
